 <?php
        require 'db.php';
    $tpl = "<img width='%spx' height='%spx' src='http://img2".$__host."/datas/%s' />";
    $tpl2 = "<img width='%spx' height='%spx' src='http://img6".$__host."/datas/%s' />";
    $code = $_GET['code'];
    $images = array();
    $torrent = array(); // name, info
    $sqlpost = "select pid,fid,title,content,postdate,postdetailurl from post where code='%s'";
    $sqlimage = "select filename, width, height from image where pid='%s';";
    $sqltorrent = "select filename, size from torrent where pid='%s';";
    $sqltorrent_files = "select filename, suffix from torrent_files where pid='%s';";
    $pid = null;
    $fid = null;
    $title = null;
    $content = null;
    $postdate = null;
    // foreach($arr as $item) foreach($arr as $k => $v)
    // 查询帖子详细信息
    $mysqli->real_query(sprintf($sqlpost, $code));
    $result = $mysqli->use_result();
    if ($result) {
            $row = $result->fetch_row();
            $pid = $row[0];
            $fid = $row[1];
            $title = $row[2];
            $content = $row[3];
            $postdate = $row[4];
            $orgurl = $row[5];
            $result->close();
        // 查询图片
        $mysqli->real_query(sprintf($sqlimage, $pid));
        $result2 = $mysqli->use_result();                            
        if ($result2) {
            while ($row = $result2->fetch_row()){
                $fname = str_replace('resource/', '', $row[0]); 
                $img = array(
                    'filename' => $fname,
                    'width' => $row[1],
                    'height' => $row[2],
                );
                array_push($images, $img);
            }
            $result2->close();
        }
        // 查询种子
        $mysqli->real_query(sprintf($sqltorrent, $pid));
        $result2 = $mysqli->use_result();                            
        if ($result2) {
            while ($row = $result2->fetch_row()){
                $fname = str_replace('resource/', '', $row[0]); 
                $imghost = 'http://img3'.$__host.'/datas';
                $t = array(
                    'filename' => $imghost.$fname,
                    'size' => $row[1],
                );
                $torrent['info'] = $t; 
                break;
            }
            $result2->close();
        }
    }else{
        die('fuck off');
    }

    
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title> <?=$title?> </title>
<meta name="keywords" content="" />
<meta name="description" content="<?=$title?>" />
<style>
body{
    background-color: #A2C672;
}    

    
</style>
<script src="/jquery.min.js"></script>
<link rel="stylesheet" type="text/css"  href="/css/main.css"  />
</head>
        
        <body>
            
          <?php
                require 'header.php';
            ?>
          <div id='post_content'>
              
            
              <h4><?=$title?></h4>
              <p><?=$content?></p>
              
            
            <div class='jjga'>
                <div class='full'>
                    <div class='picinfo'></div>
                    <?php
                        $idx = 0;
                        foreach($images as $img){
                            $imghost = 'http://img%d'.$__host.'/datas';
                            $hostn = rand(1,7);
                            $imghost = sprintf($imghost, $hostn);
                            printf('<img imgidx="%d" _width="%dpx" _height="%dpx" src="%s" />', 
                                    $idx, $img['width'], $img['height'], $imghost.$img['filename']);
                            $idx += 1;
                        }
                        $maxwidth = 130 * ($idx + 1);
                    ?>                    
                    
                </div>
                
                <div class='thumbs'>
                    <div class='scroll' num='<?=$idx+1?>' maxwidth='<?=$maxwidth?>px'>
                        <?php
                            $idx = 0;
                            $last = null;
                            foreach($images as $img){
                                $imghost = 'http://datas'.$__host.'/datas';
                                $hostn = rand(1,7);
                                $imghost = sprintf($imghost, $hostn);
                                $imgg = $imghost.$img['filename'];
                                $arr = explode('/', $imgg);
                                $arr[count($arr)-1] = 'cropsmall_'.$arr[count($arr)-1];
                                $imgg = join('/', $arr);
                                $last = $idx
                                ?>
                                <div class='item'>
                                    <a  href="javascript:void(0)" onclick="javascript: clickthumb(this, '<?=$idx?>');"> 
                                        <img idx='<?=$idx?>' width="120px" height="120px" onerror="switchimg(this)"  src='<?=$imgg?>' />
                                    </a>
                                </div>
                                <?php
                                $idx += 1;
                            }
                        ?>                    
                    </div>
                </div>
                
            </div>
            
          </div>
          
            
                            <!--a  href='<?=$orgurl?>' >原始地址</a-->
<?php
                if (empty($torrent) == FALSE){
            ?>
                <div class='torrent'>
                    <a href='<?=$torrent['info']['filename']?>' target="_blank">点击下载种子(<?=$torrent['info']['size']?>)</a>
                </div>
            <?php
                }
            ?>
           <br/> 
           <br/> 
                <div class='reply'>
                    <?php 
                        $cmsql = 'select author, content from reply where pid="%s" order by iorder asc';
                        $cmsql = sprintf($cmsql, $pid);
                        $mysqli->real_query($cmsql);
                        $result = $mysqli->use_result(); 
                        if ($result) {
                            while ($row = $result->fetch_row()){
                                $author = $row[0];
                                $content =$row[1];
                                ?>
                                <div class='replyauthor'>
                                    <?=$author?>:
                                </div>
                                <div class='replycontent'>
                                    <?=$content?>
                                </div>
                                <?php
                            }
                            $result->close();
                        }

                    ?>
                </div>
                <div class='rela'>
                    <?php
                    // 同厂商
                        $keysql = 'select tag_key ,1 from tag_rela where pid="%s"';
                        $keysql = sprintf($keysql, $pid);
                        $tag_key = '';
                        $mysqli->real_query($keysql);
                        $result = $mysqli->use_result(); 
                        if ($result) {
                            $row = $result->fetch_row();
                            $tag_key = $row[0];
                            $result->close();
                            $sp = explode('_', $tag_key);
                            $tag_key = $sp[0];
                        }
                        $cmsql = " select code, fid, title, preview, tw, th, cat from post where pid in (select pid from tag_rela where tag_key=(select tag_key from tag_rela where pid='%s') ) and pid<>'%s' order by postdate desc limit 0, 3;";
                        $cmsql = sprintf($cmsql, $pid, $pid);
                        $cmsql = sprintf($cmsql, $pid);
                        $mysqli->real_query($cmsql);
                        $result = $mysqli->use_result(); 
                        if ($result) {
                            while ($row = $result->fetch_row()){
                                $code = $row[0];
                                $fid = $row[1];
                                $title = $row[2];
                                $thumburl = $row[3];
                                $tw = $row[4];
                                $th = $row[5];
                                $cat = $row[6];
                                $ratio = 1;
                                $preview_image = sprintf($tpl2,$tw*$ratio,$th*$ratio, $thumburl);
                                $full_preview = str_replace('crop_', '', $thumburl);
                                ?>
                                <div class='rela_thumb'>
                                    <a href="post.php?code=<?=$code?>" title="<?=$title?>">
                                        <?=$preview_image?>
                                    </a>
                                </div>
                                <?php
                            }
                            $result->close();
                        }

                    ?>
                                <div class='rela_thumb'>
                                    <a href="index.php?page=1&tag=<?=$tag_key?>_1" target=_blank style='text-decoration:none; '>
                                        更多 <?=$tag_key?> ...
                                    </a>
                                </div>
                </div>
          <div id='footer'>
              <br />
              <br />
              <br />
          </div>
        
        <script>
            $(function (){
                // gallery
                var fullimages = $('.jjga .full img')
                fullimages.hide()
                var ii = fullimages.toArray()
                $(ii[0]).show()
                infopic(1)
                $('.scroll').css('width', $('.scroll').attr('maxwidth'))
                bigpicclick()
            })
            
            var  curindex = null
            function clickthumb(aa, sindex){
                infopic(parseInt(sindex) +1)
                borderimg(sindex)
                var fullimages = $('.jjga .full img')
                fullimages.hide()
                fullimages.attr('isshown', '0')
                fullimages.each(function (){
                    var ii = $(this)
                    if (ii.attr('imgidx') == sindex){
                        ii.show()
                    }
                } )
            }
            
            function nextpic(idx){
                idx += 1
                if (idx <= 0){
                    idx = <?=$last?> 
                }
                if ($('.thumbs').find('img[idx='+idx+']').size()==0){
                    idx = 0
                }
                clickthumb(null, idx)
                borderimg(idx)
            }
            
            function borderimg(idx){
                $('.thumbs img').css('border', '0')
                var tb = $('.thumbs').find('img[idx='+idx+']')
                tb.css('border', '1px solid red')
            }
            
            function switchimg(img){
                img = $(img)
                var iurl = img.attr('src').replace('cropsmall_', 'crop_')
                img.attr('src', iurl)
            }
            
            function infopic(idx){
                var info = idx + ' / ' +(parseInt( $('.scroll').attr('num')) - 1)
                $('.picinfo').html(info)
            }
            
            function moveleft(){
                $(".scroll").animate({left:'-625px'});
            }
            
            function bigpicclick(){
                //前后翻图
                var bw = 0;
                $(".full img").mousemove(function(e) {
                    bw = $(this).width()/5*2;
                    var w=e.clientX-$(this).offset().left||e.pageX-$(this).offset().left||0;
            if (w < bw) {
                $(this).css("cursor", "url(/bg11.cur), default");
                        // $(this).attr("title", "← 看上一张"); 
            } else {
                $(this).css("cursor", "url(/bg12.cur), default");
                        // $(this).attr("title", "→ 看下一张");
                    }
                });
                $(".full img").click(function(e) {
                    var cindex = $(this).attr('imgidx')
                    cindex = parseInt(cindex)
                    bw = $(this).width()/5*2;
                    var w = e.clientX-$(this).offset().left||e.pageX-$(this).offset().left||0;
                    if (w < bw) {
                        // prv
                        nextpic(cindex-2)
                    } else {
                        // next            
                        nextpic(cindex)
                    }
                    });
            }
            
        </script>
          
        </body>
        
</html>
